package com.explorati.interview.interview.chaptereight;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class TestThreadPool {

	private static int produceTaskSleepTime = 2000;

	public static void main(String[] args) {

		// 构造一个线程池
		ThreadPoolExecutor producePool = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS, new ArrayBlockingQueue(3),
				new ThreadPoolExecutor.DiscardOldestPolicy());

		// 每隔peoduceTaskSleepTime向线程池中派送一个任务
		int i = 1;
		while (true) {
			try {
				Thread.sleep(produceTaskSleepTime);
				String task = "task@" + i;
				System.out.println("put  " + task);
				// 用execute方法启动一个线程
				producePool.execute(new ThreadPoolTask(task));
				i++;
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
